package com.heima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.reggie.common.R;
import com.heima.reggie.pojo.*;
import com.heima.reggie.service.CategoryService;
import com.heima.reggie.service.DishService;
import com.heima.reggie.service.SetmealDisheService;
import com.heima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SetmealDisheService setmealDisheService;
    @Autowired
    private DishService dishService;

    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize, String name) {
        Page<Setmeal> page1 = new Page<>(page, pageSize);

        LambdaQueryWrapper<Setmeal> setm = new LambdaQueryWrapper<>();
        setm.like(StringUtils.isNotEmpty(name), Setmeal::getName, name);
        setm.orderByDesc(Setmeal::getName).orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(page1, setm);

        Page<SetmealDto> page2 = new Page<>();
        BeanUtils.copyProperties(page1, page2, "records");
        List<Setmeal> records = page1.getRecords();
        List<SetmealDto> collect = records.stream().map((rec) -> {
            SetmealDto setmealDto = new SetmealDto();
            Long id = rec.getCategoryId();

            BeanUtils.copyProperties(rec, setmealDto);
            Category byId = categoryService.getById(id);
            log.info("byid{}", byId);
            if (byId != null) {
                String name1 = byId.getName();

                setmealDto.setCategoryName(name1);
            }

            return setmealDto;
        }).collect(Collectors.toList());
        page2.setRecords(collect);

        return R.success(page2);

    }

    @PostMapping
    public R save11(@RequestBody SetmealDto setmealDto) {
        setmealService.saveDish(setmealDto);

        return R.success("保存成功");
    }
@DeleteMapping
   public R deleteByID(@RequestParam List<Long> ids){
    setmealService.deleteByID(ids);


    return R.success("删除成功");
   };

    @PostMapping("/status/{status}")
    public  R status(@RequestParam List<Long> ids, @PathVariable int status){
        setmealService.status(ids,status);

        return R.success("禁用成功");
    };
@GetMapping("/list")
    public  R list(Setmeal setmeal ){
    LambdaQueryWrapper<Setmeal> setlamd = new LambdaQueryWrapper<>();
    setlamd.eq(setmeal.getCategoryId() !=null,Setmeal::getCategoryId,setmeal.getCategoryId());
    setlamd.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
    List<Setmeal> list = setmealService.list(setlamd);



    return R.success(list);
    }
    @GetMapping("/dish/{id}")
    public R dish(@PathVariable long id   ){
        LambdaQueryWrapper<SetmealDish> set = new LambdaQueryWrapper<>();
        set.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDisheService.list(set);
        List<Dish> collect = list.stream().map((lis) -> {
            Long id1 = lis.getDishId();
            Dish byId = dishService.getById(id1);

            return byId;
        }).collect(Collectors.toList());

        return R.success(collect);
    }
@GetMapping("/{id}")
public  R setmeal(@PathVariable Long id){
    Setmeal byId = setmealService.getById(id);


    return  R.success(byId);
};
}
